#include "checkiso.h"
#include "ui_checkiso.h"

#include <QProcess>
#include <QTimer>
#include <QDebug>

CheckISO::CheckISO(QWidget *parent) :
    Base(parent),
    ui(new Ui::CheckISO)
{
    ui->setupUi(this);

    ui->textEdit->hide();
    ui->check_label->hide();

    connect(this, &CheckISO::updateCheckState, this, &CheckISO::onUpdateCheckResult);
}

CheckISO::~CheckISO()
{
    delete ui;
}

void CheckISO::showCurrentModule()
{

}

void CheckISO::start()
{
    if (m_checkState == CHECKING)
        return;

    m_checkState = CHECKING;
    Q_EMIT updateCheckState(CHECKING);


    ui->icon_label->setStyleSheet("QLabel{ border-image: url(:/icons/iso.png) 4 4 4 4 stretch stretch;}");
    ui->textEdit->hide();
    ui->check_label->setText("check ISO ...");
    ui->check_label->show();

    qInfo() << "TODO: check ISO";
    // TODO do check



    QTimer::singleShot(1000, this, [&]() {
        m_checkState = CHECKSUCCESSED;
        Q_EMIT updateCheckState(CHECKSUCCESSED);

        //m_checkState = CHECKFAILED;
        //Q_EMIT updateCheckState(CHECKFAILED);
    });
}

bool CheckISO::nextStep()
{
    if (m_checkState != CHECKSUCCESSED) {
        start();
        return false;
    }

    return true;
}

void CheckISO::onUpdateCheckResult(CheckState state)
{
    if (state == CHECKING)
        return;

    ui->check_label->setText(state == CHECKSUCCESSED ? tr("check successed") : tr("check failed"));
    ui->icon_label->setStyleSheet(state == CHECKSUCCESSED ? "QLabel{ border-image: url(:/icons/iso_check_success.png) 4 4 4 4 stretch stretch;}"
                                         : "QLabel{ border-image: url(:/icons/iso_check_failed.png) 4 4 4 4 stretch stretch;}"
                                           );
    if (state != CHECKSUCCESSED) {
        ui->textEdit->setText(tr("The image file you are using did not pass the integrity check..."));
        ui->textEdit->show();
    }
}

